@echo off
chcp 65001 >nul
cls
color 0C

echo.
echo ════════════════════════════════════════════
echo   一键修复模拟位置问题
echo ════════════════════════════════════════════
echo.

echo 检测到的设备：
adb devices -l
echo.

echo 请选择设备：
echo   1. 4f6c2e31 (真机)
echo   2. emulator-5554 (模拟器)
echo.

set /p choice=输入选项 (1 或 2): 

if "%choice%"=="1" (
    set DEVICE=-s 4f6c2e31
    echo 已选择：真机
) else if "%choice%"=="2" (
    set DEVICE=-s emulator-5554
    echo 已选择：模拟器
) else (
    echo 无效选项，使用默认设备
    set DEVICE=
)

echo.
echo ════════════════════════════════════════════
echo  开始修复
echo ════════════════════════════════════════════
echo.

echo [1/8] 停止应用...
adb %DEVICE% shell am force-stop com.example.munidingwei
timeout /t 1 >nul
echo ✅ 完成

echo.
echo [2/8] 卸载旧版本...
adb %DEVICE% uninstall com.example.munidingwei >nul 2>&1
echo ✅ 完成

echo.
echo [3/8] 编译新版本...
call gradlew assembleDebug --quiet
if errorlevel 1 (
    echo ❌ 编译失败
    pause
    exit /b 1
)
echo ✅ 完成

echo.
echo [4/8] 安装应用...
adb %DEVICE% install app\build\outputs\apk\debug\app-debug.apk
if errorlevel 1 (
    echo ❌ 安装失败
    pause
    exit /b 1
)
echo ✅ 完成

echo.
echo [5/8] 授予模拟位置权限...
adb %DEVICE% shell appops set com.example.munidingwei android:mock_location allow
echo ✅ 完成

echo.
echo [6/8] 授予定位权限...
adb %DEVICE% shell pm grant com.example.munidingwei android.permission.ACCESS_FINE_LOCATION
adb %DEVICE% shell pm grant com.example.munidingwei android.permission.ACCESS_COARSE_LOCATION
echo ✅ 完成

echo.
echo [7/8] 授予通知权限...
adb %DEVICE% shell pm grant com.example.munidingwei android.permission.POST_NOTIFICATIONS 2>nul
echo ✅ 完成

echo.
echo [8/8] 清除日志并启动...
adb %DEVICE% logcat -c
adb %DEVICE% shell am start -n com.example.munidingwei/.MainActivity
timeout /t 2 >nul
echo ✅ 完成

echo.
echo ════════════════════════════════════════════
echo  验证权限
echo ════════════════════════════════════════════
echo.

echo 模拟位置权限状态：
adb %DEVICE% shell appops get com.example.munidingwei MOCK_LOCATION
echo.

echo 定位权限状态：
adb %DEVICE% shell dumpsys package com.example.munidingwei | findstr "permission\|mock"
echo.

pause

echo.
echo ════════════════════════════════════════════
echo  测试模拟位置
echo ════════════════════════════════════════════
echo.
echo 📱 请在手机上操作：
echo.
echo    方式 1: 搜索地址
echo      1. 在搜索框输入"天安门"
echo      2. 点击"搜索地址"
echo      3. 点击任一搜索结果
echo.
echo    方式 2: 点击地图
echo      1. 点击地图任意位置
echo      → 应立即设置为模拟位置
echo.
echo    方式 3: 拖动地图
echo      1. 拖动地图到目标位置
echo      2. 看到红色标记 📍
echo      3. 点击"设置为模拟位置"按钮
echo.
echo 监控日志中...
echo 按 Ctrl+C 停止
echo ════════════════════════════════════════════
echo.

adb %DEVICE% logcat LocationService:D LocationForegroundService:D MockLocationProvider:D *:S

pause

